from django.contrib.auth.backends import ModelBackend
from django.db.models import Q

from users.models import User


def jwt_response_payload_handler(token, user=None, request=None):
    """
    重写jwt_response_payload_handler的返回,原代码至返回token
    """

    return {
        'user_id' : user.id,
        'username' : user.username,
        'token': token
    }




class UsernameModleauthenticate(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        try:
            user = User.objects.get(Q(username=username) | Q(mobile=username))
        except User.DoesNotExist:
            return None

        else:
            if user.check_password(password):
                return user